package com.ihr360.expr.function.excel;


import com.ihr360.expr.ExprError;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.ExprString;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.AbstractFunction;
import com.ihr360.expr.core.Expr;

public class MID extends AbstractFunction
{
    @Override
    public Expr evaluate(IEvaluationContext context, Expr[] args)
            throws ExprException {
        assertArgCount(args, 3);
        String str = asString(context, args[0], false);
        int start = asInteger(context, args[1], true);
        if (start < 1){
            return ExprError.VALUE;
        }
        int len = asInteger(context, args[2], true);
        if (len < 0){
            return ExprError.VALUE;
        }
        int stlen = str.length();
        if (start > stlen){
            start = stlen + 1;
        }
        if (start + len > stlen){
            len = stlen - start + 1;
        }
        if (len < 0){
            len = 0;
        }
        return new ExprString(str.substring(start - 1, start + len - 1));
    }
}
